package com.example.javaee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.javaee.common.result;
import com.example.javaee.entity.AssetOrg;
import com.example.javaee.entity.AttackTask;
import com.example.javaee.entity.MyTask;
import com.example.javaee.service.AttackTaskService;
import com.example.javaee.service.MyTaskService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/myTask")
public class MyTaskController {
    @Autowired
    private MyTaskService myTaskService;

    //分页获取用户的所有任务
    @GetMapping("/getTasks/{userId}/{pg}")
    public result<Page> page(@PathVariable Integer pg,@PathVariable int userId){
        //分页构造器
        Page<MyTask> pageInfo = new Page<>(pg,12);
        //条件构造器
        LambdaQueryWrapper<MyTask> queryWrapper = new LambdaQueryWrapper<>();
        //分页查询
        queryWrapper.eq(MyTask::getUserId, userId);
        myTaskService.page(pageInfo,queryWrapper);
        System.out.printf(String.valueOf(pageInfo));
        return result.success(pageInfo,"查询成功");
    }
    //更新任务的状态（接受，拒绝，完成）
    @PostMapping("/update")
    public result<MyTask> update(@RequestBody MyTask myTask) {
        UpdateWrapper<MyTask> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", myTask.getId());
        if(myTaskService.update(myTask, updateWrapper)){
            return result.success(null, "更新成功");
        } else {
            return result.error("更新失败");
        }
    }

}
